package com.atguigu.gulimail.product.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.atguigu.common.service.seckill.SecKillDubboService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.common.utils.R;
import com.atguigu.gulimail.product.dao.SkuInfoDao;
import com.atguigu.gulimail.product.entity.SeckillInfoVo;
import com.atguigu.gulimail.product.entity.SkuImagesEntity;
import com.atguigu.gulimail.product.entity.SkuInfoEntity;
import com.atguigu.gulimail.product.entity.SpuInfoDescEntity;
import com.atguigu.gulimail.product.service.*;
import com.atguigu.gulimail.product.vo.SkuItemSaleAttrVo;
import com.atguigu.gulimail.product.vo.SkuItemVo;
import com.atguigu.gulimail.product.vo.SpuItemAttrGroupVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService{


    @Autowired
    SkuImagesService skuImagesService;

    @Autowired
    SpuInfoDescService spuInfoDescService;

    @Autowired
    AttrGroupService attrGroupService;

    @Autowired
    SkuSaleAttrValueService skuSaleAttrValueService;

    @Autowired
    ThreadPoolExecutor executor;

    @DubboReference(check = false)
    SecKillDubboService secKillDubboService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        this.baseMapper.insert(skuInfoEntity);
    }



    @Override
    public PageUtils queryPageByParam(Map<String, Object> params) {

        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<SkuInfoEntity>();

        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            wrapper.and((w) -> {
                w.eq("sku_id", key).or().like("sku_name", key);
            });
        }

        String brandId = (String) params.get("brandId");
        if (!StringUtils.isEmpty(brandId) && !brandId.equalsIgnoreCase("0")) {
            wrapper.eq("brand_id", brandId);
        }

        String catelogId = (String) params.get("catelogId");
        if (!StringUtils.isEmpty(catelogId) && !catelogId.equalsIgnoreCase("0")) {

            wrapper.eq("catalog_id", catelogId);

        }

        String min = (String) params.get("min");
        if (!StringUtils.isEmpty(min)) {
            wrapper.ge("price", min);
        }

        String max = (String) params.get("max");
        if (!StringUtils.isEmpty(max)) {
            BigDecimal bigDecimal = new BigDecimal(max);

            if(bigDecimal.compareTo(new BigDecimal("0"))==1){

                wrapper.le("price", max);

            }
        }

        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<SkuInfoEntity> QuerySkuBySpuId(Long spuId) {

        List<SkuInfoEntity> spu_id = this.list(new QueryWrapper<SkuInfoEntity>().eq("spu_id", spuId));

        return spu_id;
    }

    @Override
    public SkuItemVo getProductBySkuId(Long skuId) throws ExecutionException, InterruptedException {
        SkuItemVo itemVo = new SkuItemVo();


        //1，查询sku的基本信息   pms_sku_info
        CompletableFuture<SkuInfoEntity> Infofuture = CompletableFuture.supplyAsync(() -> {
            SkuInfoEntity skuInfoEntity = getById(skuId);
            itemVo.setInfo(skuInfoEntity);
            return skuInfoEntity;
        }, executor);


        CompletableFuture<Void> skuItemSaleAttrVoCompletableFuture = Infofuture.thenAcceptAsync((res) -> {
            //3,获取spu的销售属性组合
            List<SkuItemSaleAttrVo> skuItemSaleAttrVo = skuSaleAttrValueService.getSpuSaleAttrBySpuId(res.getSpuId());
            itemVo.setSaleAttrVos(skuItemSaleAttrVo);
        }, executor);


        CompletableFuture<Void> spuInfoDescEntityCompletableFuture = Infofuture.thenAcceptAsync((res) -> {
            //4,获取spu的介绍
            SpuInfoDescEntity spuInfoDescEntity = spuInfoDescService.getById(res.getSpuId());
            itemVo.setDesp(spuInfoDescEntity);
        }, executor);


        CompletableFuture<Void> attrGroupVoCompletableFuture = Infofuture.thenAcceptAsync((res) -> {
            //5，获取spu的规格参数信息
            List<SpuItemAttrGroupVo> attrGroupVo = attrGroupService.getAttrGroupAndAttrsBySpuId(res.getCatalogId(), res.getSpuId());
            itemVo.setGroupAttrs(attrGroupVo);
        }, executor);


        CompletableFuture<Void> imagesCompletableFuture = CompletableFuture.runAsync(() -> {
            //6,sku的图片信息       pms_sku_images
            List<SkuImagesEntity> images = skuImagesService.getImagesBySkuId(skuId);
            itemVo.setImages(images);
        }, executor);

        CompletableFuture<Void> secKillCompletableFuture = CompletableFuture.runAsync(() -> {
            //7,Seckill的秒杀信息       pms_sku_images
            R r = secKillDubboService.getSecKillSkus(skuId);

            SeckillInfoVo data = r.getData(new TypeReference<SeckillInfoVo>() {});
            itemVo.setSeckillInfo(data);


        }, executor);



        CompletableFuture.allOf(skuItemSaleAttrVoCompletableFuture,
                                spuInfoDescEntityCompletableFuture,
                                attrGroupVoCompletableFuture,
                                imagesCompletableFuture,
                                secKillCompletableFuture).get();


        return itemVo;
    }


}










